﻿using EkStudio.com.bean.xml.reflection;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Field;
using System.Collections.Generic;
using System;

namespace EkStudio.com.bean.xml.BillChart
{
    public class BillChartModuleFieldsRoot:BillUiFormatContainer
    {
        private int maxid = 0;

        private List<BillUiFieldsItem> fields;

        [Xml]
        public int Maxid
        {
            get { return maxid; }
            set { maxid = value; }
        }
        [XmlElement]
        public List<BillUiFieldsItem> Fields
        {
            get { return fields; }
            set { fields = value; }
        }

        [XmlTransient]
        public object ParentBean
        {
            get { return null; }

            set { }
        }

        public object Clone()
        {
            BillChartModuleFieldsRoot clone = new BillChartModuleFieldsRoot();
            clone.Maxid = maxid;
            clone.fields = new List<BillUiFieldsItem>();
            if (fields != null && fields.Count > 0)
            {
                foreach(BillUiFieldsItem field in fields)
                {
                    BillUiFieldsItem it = field.Clone() as BillUiFieldsItem;
                    if (it != null)
                    {
                        clone.fields.Add(it);
                    }
                }
            }
            return clone;
        }

        public void addNode(BillUiFormatNode node, BillFormatModule format)
        {
            BillUiFieldsItem field = node as BillUiFieldsItem;
            field.LanguageTexts = this.getNLanguageText(format);
            if (this.fields == null) this.fields = new List<BillUiFieldsItem>();
            this.fields.Add((BillUiFieldsItem)node);
        }

        public void removeNode(BillUiFormatNode node)
        {
            BillUiFieldsItem field = node as BillUiFieldsItem;
            if (this.fields.Contains(field))
            {
                this.fields.Remove(field);
            }
        }

        public void moveUp(BillUiFormatNode node)
        {
            BillUiFieldsItem group = node as BillUiFieldsItem;
            int index = fields.IndexOf(group);
            if (index != -1)
            {
                int PevIndex = index - 1;
                //logger.Info("移动前当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");

                this.fields.RemoveAt(index);
                this.fields.Insert(PevIndex, group);
                //logger.Info("移动后当前[" + this.fieldsProperties[index].Caption + "] 前位是[" + this.fieldsProperties[PevIndex].Caption + "]");
            }
        }

        public void moveDown(BillUiFormatNode node)
        {
            BillUiFieldsItem group = node as BillUiFieldsItem;
            int index = this.fields.IndexOf(group);
            if (index < (this.fields.Count - 1))
            {
                int LastIndex = index + 1;
                BillUiFieldsItem LastGroup = this.fields[LastIndex];
                this.fields.RemoveAt(LastIndex);
                this.fields.Insert(index, LastGroup);
            }
        }

        public string getNLanguageText(BillFormatModule format)
        {
            string result = "";
            int index = format.getLanguageText().IndexOf("Caption");
            if (index != -1)
            {
                int length = format.getLanguageText().Length - 7;
                result = format.getLanguageText().Substring(0, length);
                int tempMaxid = maxid + 1;
                Maxid = tempMaxid;
                result = result + "Fields[" + tempMaxid + "].Caption";
            }
            return result;
        }

        public void updateLanguageText(BillFormatModule format)
        {
            if (fields != null && fields.Count > 0)
            {
                this.maxid = 0;
                foreach(BillUiFieldsItem field in fields)
                {
                    field.LanguageTexts = getNLanguageText(format);
                }
            }
        }
    }
}
